import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import API from '@/utils/commonRequest.js'

import {
    checkSteelProtectionTestTaskData, //保存--钢保
    saveConcreteCheckAduit,//保存--回弹
    getBaseDepartmentUser,//查人
} from "@/api/request.js";
export default {
    data() {
        return {
            visible: "",
            // WBS
            WBSShow: false,
            // WBS
            loading: false,
            isLock: false, //保存按钮是否可编辑
            formData: {
                fId: "",// 当前数据fid
                aduitStatus: "",//列表页流程状态
                aduitResult: "",//审核状态  同意 驳回 撤销
                aduitNote: "",//审核意见
                aduitDates: "",//审核日期
                aduitUserId: "",//下一节点审核人id
                aduitUserName: "",//下一节点审核人
                userId: "",// 当前登陆用户ID
                userName: ""// 当前登陆用户名称
            },
            formObj: formJs.formObj,
            btnShow: true,
            currentDetail: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            selected: [], //多选接值
            primaryInfo: null,
            personList: [],//人列表
            userInfo: null,//当前用户
            userNature: "",
        };
    },
    components: {
        publicForm,
        FormLyout,
    },
    created() {
        this.getData();
    },

    mounted() {
        actions.onGlobalStateChange((state, prev) => {
            this.userInfo = prev.currentUser;
            console.log("userInfo", this.userInfo);
        }, true);
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
                // console.log(78789,this.userNature );
            }
        }).catch((err) => {
            console.log("API", err)
        })
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_rebarRecordApproval"));
        // console.log("primaryInfo", this.primaryInfo);
        console.log("aduitStatus", this.primaryInfo.data.aduitStatus);
        this.btnShow = true;
        this.formObj[3].disabled = false;
        this.formObj[3].rules[0].required = true;
        if (this.primaryInfo.status == 1) {
            var that = this;
            if (that.primaryInfo.data.aduitStatus == "1") {
                that.formObj[3].label = "下一节点记录人"
            } else if (that.primaryInfo.data.aduitStatus == "2") {
                that.formObj[3].label = "下一节点复核人"
            } else if (that.primaryInfo.data.aduitStatus == "3") {
                that.formObj[3].label = "下一节点审核人"
            } else if (that.primaryInfo.data.aduitStatus == "4") {
                that.formObj[3].label = "下一节点批准人"
            } else if (that.primaryInfo.data.aduitStatus == "5") {
                that.formObj[3].label = "下一节点监理人"
            } else if (that.primaryInfo.data.aduitStatus == "6") {
                this.formObj[3].disabled = true;
                this.formObj[3].rules[0].required = false;  
            }

            if (that.primaryInfo.data.aduitStatus == "5" && this.userNature == "中心试验室") {
                this.formObj[3].disabled = true;
                this.formObj[3].rules[0].required = false;
            }

            setTimeout(() => {
                that.getDetail(that.primaryInfo.data);
            }, 100);
        }
    },
    destroyed() {
        window.sessionStorage.removeItem("GxEmbedded_rebarRecordApproval");
    },
    methods: {
        // 详情
        getDetail(row) {
            // console.log("详情", row.fId, row.aduitStatus, this.timeYmdHms, this.userInfo.id, this.userInfo.userName);
            this.formData = {
                fId: row.fId,// 当前数据fid
                aduitStatus: row.aduitStatus,//列表页流程状态
                aduitResult: "同意",//审核状态  同意 驳回 撤销
                aduitNote: "",//审核意见
                aduitDates: this.timeYmdHms,//审批时间，默认当前
                aduitUserId: "",//下一节点审核人id
                aduitUserName: "",//下一节点审核人
                userId: this.userInfo.id, // 当前登录人id
                userName: this.userInfo.userName, // 当前登录人姓名
            };

            if (row.aduitStatus == "5") {
                if (this.primaryInfo.saveNum == "rebar") {
                    this.getPerson(row.fSupervisionid);
                } else if (this.primaryInfo.saveNum == "rebound") {
                    this.getPerson(row.fJlid);
                }
            } else {
                this.getPerson(row.fCompanyid);
            }
        },
        // 查人
        getPerson(id) {
            var obj = {
                fCompanyid: id,
            };
            this.personList = []
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.childList[0].personList;
                        var personLists = [];
                        arr.map((item) => {
                            personLists.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });
                        });
                        this.formObj[3].options = personLists;
                        this.personList = personLists;
                    } else {
                        this.$message.error({
                            content: "查人" + res.msg,
                            duration: 1000
                        });
                        return false;
                    }
                })
        },


        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            // 审批状态
            if (b == "aduitResult") {
                if (data != "同意") {
                    this.formObj[3].disabled = true;
                    this.formObj[3].rules[0].required = false;
                    this.formData.aduitUserName = "";
                    this.formData.aduitUserId = "";
                } else {
                    this.formObj[3].disabled = false;
                    this.formObj[3].rules[0].required = true;
                }
            }
            // 下一节点审批人
            if (b == "aduitUserName") {
                this.personList.map(item => {
                    if (data == item.label) {
                        this.formData.aduitUserId = item.value;
                    }
                })
            }
            return data, a, b;
        },
        inputFocus(index, prop) {
            // console.log("inputFocus", index, prop);
            return index, prop;
        },
        clickDialog(index, prop) {
            // console.log("clickDialog", index, prop);
            return index, prop;
        },
        yj_saveForm() {
            var save = {};
            save = {
                fId: this.formData.fId, // 当前数据fid
                aduitStatus: this.formData.aduitStatus,//列表页流程状态
                aduitResult: this.formData.aduitResult,//审核状态  同意 驳回 撤销
                aduitNote: this.formData.aduitNote,//审核意见
                aduitDates: this.formData.aduitDates,//审批时间，默认当前
                aduitUserId: this.formData.aduitUserId,//下一节点审核人id
                aduitUserName: this.formData.aduitUserName,//下一节点审核人
                userId: this.userInfo.id, // 当前登录人id
                userName: this.userInfo.userName, // 当前登录人姓名
            };
            if (this.$refs.publicForm.submitForm()) {
                if (this.primaryInfo.saveNum == "rebar") {
                    this.save_rebar(save);
                } else if (this.primaryInfo.saveNum == "rebound") {
                    this.save_rebound(save);
                }
            } else {
                console.log("error");
            }
        },
        // 钢保--保存
        save_rebar(data) {
            this.loading = true;
            this.isLock = true
            checkSteelProtectionTestTaskData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$router.push({
                            path: "/rebarThicknessManage/rebarRecordApproval",
                        });
                        this.isLock = false
                        this.$message.success({
                            content: '保存成功',
                            duration: 1000
                        });
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error({
                            content: "钢保审核保存" + res.msg,
                            duration: 1000
                        });
                    }
                })
                .catch((error) => {
                    console.log("钢保审批流程保存出错", error);
                });
        },
        // 回弹--保存
        save_rebound(data) {
            this.loading = true;
            this.isLock = true
            saveConcreteCheckAduit(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$router.push({
                            path: "/reboundStrengthManage/reboundRecordApproval",
                        });
                        this.isLock = false
                        this.$message.success({
                            content: '保存成功',
                            duration: 1000
                        })
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error({
                            content: "回弹审核保存" + res.msg,
                            duration: 1000
                        })
                        console.log("回弹审批流程接口报错");
                    }
                })
                .catch((error) => {
                    console.log("回弹审批流程保存出错", error);
                });
        },

        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
    },
};